<?php

namespace App\Http\Controllers\Api;

use App\Models\Brand;
use App\Transformers\BrandTransformer;
use Illuminate\Support\Facades\Cache;

class BrandsController extends Controller
{
    public function index()
    {
        $brands = Brand::query()->orderBy('tag')->get(['id', 'name', 'tag', 'count']);

        // 缓存品牌 id 的顺序，后面查询商品列表使用，保留一小时
        Cache::put('ordered_brand_id', implode(',', $brands->pluck('id')->all()), 60);

        $grouped = $brands->groupBy('tag')->map(function ($item, $key) {
            return [
                'tag' => $key,
                'list' => $item,
            ];
        });

        return $this->response->array($grouped->values()->all());
    }
}
